//获取Miniui datagrid列名
function getColumns(columns) {
	var cols = [];
	for(var i = 0; i < columns.length; i++) {
		var column = columns[i];
		if(column.visible) {
			var col = {
				header: column.header,
				field: column.field,
				type: column.type
			};
			if(column.columns) {
				col.columns = getColumns(column.columns);
			}
			cols.push(col);
		}

	}
	return cols;
}
//随机颜色
function random(min,max){
	if(isNaN(min) || isNaN(max)){
		return null;
	}
	if(min > max){
		min ^= max;
		max ^= min;
		min ^= max;
	}
	return (Math.random() * (max - min) | 0) + min;
}
function randomColor(){
	var color="#";
	var colorArr=['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];
	for(var i=0;i<6;i++){
		color += colorArr[random(0,16)];
	}
	return color;
}
//提示
function showMsg(text, state, time, x, y, callback) {
	mini.showTips({
		content: text,
		state: state,
		x: x,
		y: y,
		timeout: time
	});
	if(callback) {
		setTimeout(callback, time);
	}
}
//根据itemCode从localStorage中获得数据字典表
function getDict(itemCode) {
	var storage = window.localStorage;
	var json = storage.getItem("dictData");
	var jsonObj = JSON.parse(json);
	var jsonstr = "[]";
	var jsonArray = eval('(' + jsonstr + ')');
	$.each(jsonObj, function(key, val) {
		if(val.itemCode == itemCode) {
			var jsonTemp = {
				"id": val.dictValue,
				"text": val.dictCode,
				"sort": val.sort
			};
			jsonArray.push(jsonTemp);
		}
	});
	return jsonArray;
}

//根据itemCode,dictValue从localStorage中获得数据字典表
function getDictName(itemCode, dictValue) {
	var storage = window.localStorage;
	var json = storage.getItem("dictData");
	var jsonObj = JSON.parse(json);
	var jsonstr = "";
	$.each(jsonObj, function(key, val) {
		if(val.itemCode == itemCode && val.dictValue == dictValue) {
			jsonstr = val.dictCode;
		}
	});
	return jsonstr;
}

//获取当前年份向前推10年的数据集合，由近到远的排序
function getYearCombox(npl) {
	if(!npl || null == npl) {
		npl = 10;
	}
	var yearArray = new Array();
	var currentyear = new Date().getFullYear(); //获取当前年份
	for(var i = currentyear; i >= currentyear - npl; i--) {
		var yearObj = new Object();
		yearObj.id = i;
		yearObj.text = i + "年";
		yearArray.push(yearObj);
	}
	return yearArray;
}

//获取当前年份向前推到2011年的数据集合，由近到远的排序
function getYearComboxFor2011() {
	var yearArray = new Array();
	var currentyear = new Date().getFullYear(); //获取当前年份
	for(var i = currentyear; i >= 2011; i--) {
		var yearObj = new Object();
		yearObj.id = i;
		yearObj.text = i + "年";
		yearArray.push(yearObj);
	}
	return yearArray;
}

//获取省份，每次调用getProvNameByCode(provCode)需首先，加载此函数
var data = null;
var provArr = null;

function getProvName() {
	provArr = new Array();
	$.ajax({
		url: basePath + "org/provincelist",
		type: "post",
		async: false,
		success: function(text) {
			data = JSON.parse(text);
			data = data.map(function(item) {
				provArr.push(item.orgCode + "," + item.orgName);
				//provArr.push(item.orgName);
			});
		}
	});
}
//省份编码转换为省份
function getProvNameByCode(provCode) {
	var prov = JSON.stringify(provArr);
	var pro = JSON.parse(prov);
	var orgCode = "";
	var province = "";
	var proStr = null;
	var provinceArr = new Array();
	//alert(provCode.length);
	if(provCode != "" && provCode.length > 2) {
		if(provCode.indexOf(",") != -1) {
			proStr = provCode.split(",");
		} else {
			proStr = provCode.split("、");
		}
	} else {
		if(provCode != "" && provCode.length == 2) {
			orgCode = provCode + "0000";
		} else {
			orgCode = provCode;
		}

	}
	//alert(proStr);
	if(proStr != null) {
		for(var j = 0; j < proStr.length; j++) {
			if(proStr[j].length == 2) {
				orgCode = proStr[j] + "0000";
			} else {
				orgCode = proStr[j];
			}
			for(var i = 0; i < pro.length; i++) {
				var provv = pro[i].split(","); //分割从redis
				if(provv[0] == orgCode) {
					provinceArr.push(provv[1]);
				}
			}
		}
		return provinceArr;

	} else {
		for(var i = 0; i < pro.length; i++) {
			var provv = pro[i].split(","); //分割从redis 	
			if(provv[0] == orgCode) {
				return provv[1];
			}
		}
	}
	return provCode;
}
//格式化金钱
function formatNumber(str) {
	return str.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
//点击div区域防止冒泡调用
function stopPropagation(e) {
	if(e.stopPropagation)
		e.stopPropagation();
	else
		e.cancelBubble = true;
}

//桩号转换
function stakeTransformation(stakeValue) {
	if(typeof(stakeValue) == "undefined") {
		return "";
	}
	if(typeof(stakeValue) != "number" && stakeValue == "") {
		return "";
	}
	var mile = parseFloat(stakeValue);
	var s = mile.toFixed(3).toString();
	var a = s.split(".");
	if(null == a || a.length == 0) {
		return "";
	} else if(a.length == 1) {
		var zs = a[0];
		return zs;
	} else if(a.length == 2) {
		var zs1 = a[0];
		var xs = a[1];
		var rs = zs1 + "+" + xs;
		return 'K' + rs;
	}
	return "";
}

jQuery.fn.maxLength = function(max) {
	this.each(function() {
		var type = this.tagName.toLowerCase();
		var inputType = this.type ? this.type.toLowerCase() : null;
		if(type == "input" && inputType == "text" || inputType == "password") {
			//Apply the standard maxLength  
			this.maxLength = max;
		} else if(type == "textarea") {
			this.onkeypress = function(e) {
				var ob = e || event;
				var keyCode = ob.keyCode;
				var hasSelection = document.selection ? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;
				return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);
			};
			this.onkeyup = function() {
				if(this.value.length > max) {
					this.value = this.value.substring(0, max);
				}
			};
		}
	});
};
/**
 * 特殊文本框长度设置
 * @returns
 */
function specialInputLengthSet() {
	//限制input框 最多输入50个字符
	$('input').maxLength(50);
	//备注信息最多140个字符
	$('textarea').maxLength(140);
	//配置地址栏最多输入300字符
	$("input[name='moduleLink']").maxLength(300);
}

$(function() {
	specialInputLengthSet();
	/*miniui初始化后的按钮样式事件*/
	$(".mini-button-icon-text").each(function() {
		//查询
		if($(this).find("span").hasClass("icon-search")) {
			$(this).css("background", "#1D9DE1");
			/*			$(this).css("border-radius", "2px");//添加圆角属性
			 */
			$(this).find("span").css("color", "#fff");
		}
		/*else if($(this).find("span").hasClass("icon-save")){
			$(this).css("background", "#1D9DEE");
			$(this).find("span").css("color", "#ffffff");
		}*/

	});
	/*miniui初始化前的按钮样式事件*/
	$(".mini-button").each(function() {
		var icoonCls = $(this).attr("iconCls");
		//查询
		if(icoonCls == "icon-search") {
			$(this).css("background", "#1D9DE1");
			$(this).css("color", "#ffffff");
			$(this).attr("iconCls", "fa fa-search");
		}
		/*else if(icoonCls == "icon-save") {
			$(this).css("background", "#1D9DEE");
			$(this).css("color", "#ffffff");
			$(this).attr("iconCls", "fa fa-save");
		}*/

	});
});

/*自定义vtype,校验是否存在特殊字符的验证，适用于Input的校验*/
mini.VTypes["checkWordInputErrorText"] = "存在非法字符";
mini.VTypes["checkWordInput"] = function(v) { //处理特殊字符后，形成不了存储型xxs需要的格式       
	if(v == null || v.trim().length == 0) {
		return true;
	}
	var re = new RegExp("^[^<>/|'\@#$%^&*=|{}':;',?~！#￥……*|{}【】《》‘；：”“’。，、]+$");
	if(re.test(v)) {
		return true;
	} else {
		return false;
	}
}

/*自定义vtype,校验是否存在特殊字符的验证，适用于area的校验*/
mini.VTypes["checkWordAreaErrorText"] = "存在非法字符";
mini.VTypes["checkWordArea"] = function(v) { //文本只能处理指定关键字，特殊符号在文本中会用到，这里不做处理            
	if(v.indexOf("alert") != -1 || v.indexOf("script") != -1 || v.indexOf("img") != -1 || v.indexOf("javascript") != -1 || v.indexOf("onload") != -1 || v.indexOf("vbscript") != -1 || v.indexOf("expression") != -1 || v.indexOf("eval") != -1 || v.indexOf("onerror") != -1) {
		return false;
	}
	return true;
}

function getFinereportUrl() {
	var storage = window.localStorage;
	var finereportUrl = storage.getItem("finereportUrl");
	if(finereportUrl == "" || finereportUrl == null || finereportUrl == undefined || finereportUrl.replace(/(^s*)|(s*$)/g, "").length < 1) {
		$.ajax({
			type: "get",
			url: basePath + "freport/getfreportUrl",
			async: false,
			success: function(resultData) {
				finereportUrl = resultData;
				storage.setItem("finereportUrl", finereportUrl);
			},
			error: function() {
				showMsg("请求失败,请稍候重试!", "warning", 1200, "center", "center");
			}
		});
	}
	return finereportUrl;
}
/*
 * js中,格式化为两位小数
 */
function numberFormatTwo(e) {
	var num = null;
	if(e == 100) {
		return e;
	}
	if(e == null || e == "") {
		num = 0;
	} else {
		num = e;
	}
	if(e < 0.01) {
		return e;
	}
	if(isNaN(num)) {
		console.log("不是数字" + e);
		return e;
	}
	var datanumber = Math.round(num * 100) / 100;
	datanumber = datanumber.toFixed(2);
	return datanumber;
}